package com.echat.serviceapigateway.security.service.impl;

import com.echat.serviceapigateway.security.mapper.TokenHistoryMapper;
import com.echat.serviceapigateway.security.model.authentication.Token;
import com.echat.serviceapigateway.security.model.authentication.TokenHistory;
import com.echat.serviceapigateway.security.service.TokenHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by chenmohan on 2018/08/31
 *
 * @description
 */
@Service
public class TokenHistoryServiceImpl implements TokenHistoryService {

    private static ExecutorService taskExecutors = Executors.newFixedThreadPool(2);

    @Autowired
    TokenHistoryMapper tokenHistoryMapper;

    @Override
    public void fileTokenHistory(Token token) {
        TokenHistory tokenHistory = new TokenHistory();
        BeanUtils.copyProperties(token, tokenHistory);
        tokenHistory.setId(null);
        try {
            taskExecutors.submit(() -> {
                tokenHistoryMapper.insertSelective(tokenHistory);
            }).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
